package org.obeonetwork.dsl.spem.extension.ifest.ontology;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.openrdf.OpenRDFException;

import org.openrdf.model.Graph;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.GraphImpl;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;
import org.openrdf.repository.config.RepositoryConfigException;
import org.openrdf.repository.manager.LocalRepositoryManager;
import org.openrdf.repository.manager.RemoteRepositoryManager;
import org.openrdf.repository.manager.RepositoryManager;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.rdfxml.RDFXMLWriter;

public class OWLIMTool {
	public static void addOWL(String owlFilePath, String baseURI) {
		String sesameServer = "http://localhost:8080/openrdf-sesame";
		String repositoryID = "ifestOWL";
		File file = new File(owlFilePath);
		// Connect Repository

		RepositoryManager repositoryManager = new RemoteRepositoryManager(
				sesameServer);
		try {
			repositoryManager.initialize();

			Repository myRepository = (Repository) repositoryManager
					.getRepository(repositoryID);
			ValueFactory f = myRepository.getValueFactory();
			RepositoryConnection con = myRepository.getConnection();
			URI context = f.createURI(baseURI);
			try {
				// Use the repository
				con.add(file, baseURI, RDFFormat.RDFXML, context);
				//
				// URL url = new URL("http://example.org/example/remote");
				// con.add(url, url.toString(), RDFFormat.RDFXML);

			} finally {
				// Shutdown connection, repository and manager
				con.close();
				myRepository.shutDown();
				repositoryManager.shutDown();
			}

		} catch (OpenRDFException e) {
			// handle exception
		} catch (IOException e) {
			// handle io exception
		}
	}

	@SuppressWarnings("finally")
	public static List<String> getOntologyList() {
		List<String> contexts = new ArrayList<String>();

		String sesameServer = "http://localhost:8080/openrdf-sesame";
		String repositoryID = "ifestOWL";
		// Connect Repository

		RepositoryManager repositoryManager = new RemoteRepositoryManager(
				sesameServer);
		try {
			repositoryManager.initialize();

			Repository myRepository = (Repository) repositoryManager
					.getRepository(repositoryID);
			ValueFactory f = myRepository.getValueFactory();
			RepositoryConnection con = myRepository.getConnection();
			try {
				RepositoryResult<Resource> contextResource = con
						.getContextIDs();
				for (Resource s : contextResource.asList()) {
					contexts.add(s.toString());

				}
			} finally {
				// Shutdown connection, repository and manager
				con.close();
				myRepository.shutDown();
				repositoryManager.shutDown();
				return contexts;
			}
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (RepositoryConfigException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	@SuppressWarnings("finally")
	public static List<String> getOWL(String context) {
		List<String> result = new ArrayList<String>();
		String sesameServer = "http://localhost:8080/openrdf-sesame";
		String repositoryID = "ifest";
		// Connect Repository

		RepositoryManager repositoryManager = new RemoteRepositoryManager(
				sesameServer);
		try {
			repositoryManager.initialize();

			Repository myRepository = (Repository) repositoryManager
					.getRepository(repositoryID);
			ValueFactory f = myRepository.getValueFactory();
			RepositoryConnection con = myRepository.getConnection();
			try {
				// Get all resource in the context
				URI r = f.createURI(context);
				RepositoryResult<Statement> statements = con.getStatements(
						null, null, null, true, r);
				try {
					while (statements.hasNext()) {
						Statement st = statements.next();
						result.add(st.getSubject().toString());
					}
				} finally {
					statements.close();
				}
			} finally {
				// Shutdown connection, repository and manager
				con.close();
				myRepository.shutDown();
				repositoryManager.shutDown();
				return result;
			}
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (RepositoryConfigException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

/**
 * 
 * @param context
 * @param fileName
 *  This operation will get the ontology from the input context and save to file.
 */
	@SuppressWarnings("finally")
	public static void getOWL(String context, String fileName) {

		String sesameServer = "http://localhost:8080/openrdf-sesame";
		String repositoryID = "ifest";
		// Connect Repository

		RepositoryManager repositoryManager = new RemoteRepositoryManager(
				sesameServer);
		try {
			repositoryManager.initialize();

			Repository myRepository = (Repository) repositoryManager
					.getRepository(repositoryID);
			ValueFactory f = myRepository.getValueFactory();
			RepositoryConnection con = myRepository.getConnection();
			try {
				// Get all resource in the context
				URI r = f.createURI(context);

				// Export all statements in this context to file
				File file = new File(fileName);
				FileOutputStream outstream = new FileOutputStream(file, true);
				RDFHandler rdfxmlWriter = new RDFXMLWriter(outstream);
				con.exportStatements(null, null, null, false, rdfxmlWriter, r);
				outstream.close();
			} finally {
				// Shutdown connection, repository and manager
				con.close();
				myRepository.shutDown();
				repositoryManager.shutDown();
				return;
			}
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RepositoryConfigException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
	}
}
